-
Classes: Example of using
class_nameto help code cohesion and safety .-
Beginner friendly, very useful.
-
Class
-
class_name MyClass -
Class Constructor.
-
func _init() -> void: -
Can pass parameters.
-
Inner Class
-
Explanation of use, etc: Reddit post .
class Test extends Node3D:
pass
class Test extends Hitbox:
pass
-
Inner Class Constructor.
-
func _init() -> void: -
Can pass parameters.
-
Accessing a variable inside a Node or Class
-
With a dot:
-
node.variable_name
-
-
With a string:
-
node["variable_name"]
-
Keyword
super()
-
"super() is used to call the parent's class method from within the child overriding method."
class A
func say():
print("A")
class B
extends A
func say():
super() # will call A.say()
print("B")
-
B.new().say(): will print A then B.
Information transfer via Script inheritance (libraries / class)
-
Method with
extends "res://path"orextends _class_name_:-
Advantages:
@export varfunctionalities work normally. You can call all functions and properties directly, without accessing them asmy_library._function-or-property_name. -
Disadvantages: Only one 'extends' per script, creating strong dependencies on the library for scripts inheriting this way.
-
-
Method with
var my_library = _class_name_.new():-
Advantages: Allows your own 'extends', making scripts that inherit libraries this way have weak dependency on the library.
-
Disadvantages: You need to use
my_library._function-or-property_nameevery time you want to use a resource in the library.@export varfunctionalities inside the class do not work. You cannot@export varin a script and use it as a class because scripts/classes are Objects and cannot be exported.
-
-
Method with
@export var my_resource : Resource:-
Advantages: Very flexible, allowing resources contained in the Resource to be used globally or locally.
-
Disadvantages: Requires 'extends Resource', limiting usability in some cases. The interface is unintuitive and requires practice.
-
-
Method with Dictionary or Array:
-
Advantages: Easy to implement, compact.
-
Disadvantages: Less versatile, may require scanning the Dictionary/Array depending on the task.
-